<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="内容" prop="content">
        <el-input
            v-model="queryParams.content"
            clearable
            maxlength="50"
            placeholder="请输入内容"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择状态">
          <el-option label="已读" value="1"></el-option>
          <el-option label="未读" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['train:feedback:remove']"
            :disabled="multiple"
            icon="Delete"
            plain
            type="danger"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="内容" prop="content" show-overflow-tooltip width="300"/>
      <el-table-column align="center" label="内容图片" prop="picUrl" width="110">
        <template #default="scope">
          <image-preview
              v-if="scope.row.picUrl != null && scope.row.picUrl != ''"
              :src="scope.row.picUrl"
              fit="cover"
              preview-teleported="true"
              style="width:40px;object-fit: cover; height:40px;"/>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="提交人" prop="nickName"/>
      <el-table-column align="center" label="提交时间" prop="createTime"/>
      <el-table-column align="center" label="联系方式" prop="phonenumber"/>
      <el-table-column align="center" label="状态" prop="status">
        <template #default="scope">
          <el-tag v-if="scope.row.status == '1'" style="width:50px" type="success">已读</el-tag>
          <el-tag v-else style="width:50px" type="error">未读</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="回复人" prop="updateBy"/>
      <el-table-column align="center" label="回复时间" prop="updateTime"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template #default="scope">
          <el-button
              v-hasPermi="['train:feedback:edit']"
              icon="Edit"
              type="text"
              @click="handleUpdate(scope.row)"
          >回复
          </el-button>
          <el-button
              v-hasPermi="['train:feedback:remove']"
              icon="Delete"
              type="text"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

    <!-- 添加或修改意见反馈对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="600px">
      <el-form ref="feedbackRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col>
            <el-form-item label="反馈内容" label-width="100">
              <el-input
                  v-model="form.content"
                  :disabled="true"
                  :min-height="192"
                  :rows="5"
                  type="textarea"
              />
            </el-form-item>
          </el-col>
          <el-col v-if="form.picUrl != null && form.picUrl != ''">
            <el-form-item label="内容图片" label-width="100px" prop="picUrl" style="">
              <image-preview :src="form.picUrl" fit="cover" preview-teleported="true"
                             style="width:150px;object-fit: cover; height:150px;;"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="反馈回复" label-width="100" prop="reply">
              <el-input
                  v-model="form.reply"
                  :min-height="192"
                  :rows="5"
                  maxlength="100"
                  show-word-limit
                  type="textarea"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="Feedback" setup>
import {delFeedback, getFeedback, listFeedback, updateFeedback, updateFeedbackStatus} from "@/api/feedback/feedback";

const {proxy} = getCurrentInstance();

const feedbackList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    content: null,
    status: null,
  },
  rules: {
    reply: [
      {required: true, trigger: "change", message: "请输入反馈内容"},
      {pattern: /^\S*$/, message: '不能添加空格'},
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询意见反馈列表 */
function getList() {
  loading.value = true;
  listFeedback(queryParams.value).then(response => {
    feedbackList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    content: null,
    status: "0",
    delFlag: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null
  };
  proxy.resetForm("feedbackRef");
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加意见反馈";
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value
  getFeedback(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "回复意见反馈";
  });
  // 变更已读状态
  updateFeedbackStatus(id).then(response => {
    getList();
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["feedbackRef"].validate(valid => {
    if (valid) {
      updateFeedback(form.value).then(response => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const ida = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除该意见反馈？').then(function () {
    return delFeedback(ida);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

getList();
</script>
